import {
  Body,
  Controller,
  Delete,
  Get,
  Patch,
  Param,
  Post,
  Put,
} from '@nestjs/common';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Websites, WebsitesDocument } from 'src/db/schemas/websites.schema';
import { DeleteDto } from 'src/dto/delete.dto';
import { ResponseBodyDto } from '../../dto';
import { UpdateWebsiteDto } from './dto/update-websites.dto';
import { CreateWebsiteDto } from './dto/create-websites.dto'
import { QueryWebsitesAllDto, QueryWebsitesPaginationDto } from './dto/query-websites.dto';
import { ReturnWebsitesAllDto, ReturnWebsitesPaginationDto } from './dto';
import { WebsitesService } from './websites.service';
import { addOne, deleteByIds, getAll, getOneById, queryPagination, toFuzzyParams, updateOneById } from 'src/utils';
import { WebsitesDto } from 'src/db/dto';
import { Public } from 'src/common/guards';

@ApiTags('网站地址')
@Controller('websites')
@ApiBearerAuth('Authorization')
export class WebsitesController {
  constructor(
    private readonly websitesService: WebsitesService,
    @InjectModel(Websites.name) private websitesModel: Model<WebsitesDocument>,
  ) { }

  @Post('getWebsitesPagination')
  @ApiOkResponse({
    description: '网站分页',
    type: ReturnWebsitesPaginationDto,
  })
  @ApiOperation({
    summary: '获取网站分页',
  })
  getWebsitePagination(@Body() queryWebsitesDto: QueryWebsitesPaginationDto) {
    const { pageSize, current, ...other } = queryWebsitesDto;
    return queryPagination(
      this.websitesModel,
      toFuzzyParams(other),
      {
        pageSize,
        current,
      },
    );
  }

  @Public()
  @Post('getAllWebsitesByCondition')
  @ApiOkResponse({
    description: '所有网站',
    type: ReturnWebsitesAllDto,
  })
  @ApiOperation({
    summary: '根据条件获取所有网站',
  })
  getAllWebsitesByCondition(@Body() queryWebsitesDto: QueryWebsitesAllDto) {
    return getAll(
      this.websitesModel,
      toFuzzyParams(queryWebsitesDto),
    );
  }

  @Put('addWebsite')
  @ApiOkResponse({
    type: ResponseBodyDto
  })
  @ApiOperation({
    summary: '添加网站',
  })
  addWebsite(@Body() createWebsiteDto: CreateWebsiteDto) {
    return addOne(this.websitesModel, createWebsiteDto)
  }

  @Put('updateWebsite/:_id')
  @ApiOkResponse({
    description: '更新结果',
    type: ResponseBodyDto,
  })
  @ApiOperation({
    summary: '更新网站',
  })
  updateWebsite(
    @Param('_id') _id: string,
    @Body() updateWebsiteDto: UpdateWebsiteDto,
  ) {
    updateOneById(this.websitesModel, _id, updateWebsiteDto);
  }

  @Delete('deleteWebsites')
  @ApiOkResponse({
    description: '删除结果',
    type: ResponseBodyDto,
  })
  @ApiOperation({
    summary: '删除网站',
  })
  deleteWebsites(@Body() deleteDto: DeleteDto) {
    deleteByIds(this.websitesModel, deleteDto.ids);
  }

  @Get('getWebsiteById/:_id')
  @ApiOkResponse({
    description: '网站',
    type: WebsitesDto,
  })
  @ApiOperation({
    summary: '获取网站',
  })
  getWebsiteById(@Param('_id') _id: string) {
    return getOneById(this.websitesModel, _id);
  }
}
